<?

class Container {

	private $__show_container;
	private $__callback_begin;
	private $__callback;
	private $__callback_end;
	private $_objects = array ();
	private $_options = array ();
	private $_variables = array ();
	private $_parent = null;
	private $_comment_begin = null;
	private $_comment_end = null;

	public function Container ($options = array ()) {

		foreach ($options as $option => $value) $this->$option = $value;

		$this->set_option ("callback_begin", true);
		$this->set_option ("callback_end", true);

		$this->set_option ("callback_pre", false);
		$this->set_option ("callback_post", false);
	}

	public function set_option ($option, $value) {

		$this->_options[$option] = $value;
	}

	public function get_option ($option) {

		return $this->_options[$option];
	}

	public function set_variable ($variable, $value) {

		$this->_variables[$variable] = $value;
	}

	public function get_variable ($variable) {

		return $this->_variables[$variable];
	}

	public function insert_container ($container, $location=null) {

		if (gettype ($container) != "object") return;

		$container->set_parent ($this);

		if (isset ($location)) {

			$trail = array_splice ($this->_objects, $location);
			array_push ($this->_objects, $container);
			$this->_objects = array_merge ($this->_objects, $trail);
		} else array_push ($this->_objects, $container);
	}

	public function prepend_container ($container) {

		if (gettype ($container) != "object") return;

		$container->set_parent ($this);
		array_unshift ($this->_objects, $container);
	}

	public function get_parent () {

		return $this->_parent;
	}

	private function set_parent ($parent) {

		$this->_parent = $parent;
	}

	public function show_container () {

		if (function_exists ($this->__show_container)) {

			$callback = $this->__show_container;
			$callback ();
		} else $this->_show_container ();
	}

	private function _set_show_container ($callback) {

		$this->__show_container = $callback;
	}

	private function _show_container () {

		if ($this->_comment_begin) {
		
			print ("\n\n<!-- #######################\n\n");
			print ("     BEGIN CONTAINER COMMENT START\n\n");
			print ($this->_comment_begin);
			print ("\n\n     BEGIN CONTAINER COMMENT END\n\n");
			print ("     ####################### -->\n\n");
		}

		if ($this->get_option ("callback_begin"))

			$this->callback_begin ();
		if ($this->get_option ("callback_pre"))

			$this->callback ();

		foreach ($this->_objects as $object) {

			if (gettype ($object) == "object")

				$object->show_container ();
		}

		if ($this->get_option ("callback_post"))

			$this->callback ();
		if ($this->get_option ("callback_end"))

			$this->callback_end ();

		if ($this->_comment_end) {

			print ("\n\n<!-- #######################\n\n");
			print ("     END CONTAINER COMMENT START\n\n");
			print ($this->_comment_end);
			print ("\n\n     END CONTAINER COMMENT FINISH\n\n");
			print ("     ####################### -->\n\n");
		}
	}

	private function callback_begin () {

		if (function_exists ($this->__callback_begin)) {

			$callback = $this->__callback_begin;
			$callback ($this);
		} else $this->_callback_begin ();
	}

	private function callback () {

		if (function_exists ($this->__callback)) {

			$callback = $this->__callback;
			$callback ($this);
		} else $this->_callback ();
	}

	private function callback_end () {

		if (function_exists ($this->__callback_end)) {

			$callback = $this->__callback_end;
			$callback ($this);
		} else $this->_callback_end ();
	}

	private function _callback_begin () {

		$class = ($c = $this->get_variable ("class")) ? $c : "css_outline_a";
		print <<<HTML

<div class="{$class}" {$this->get_variable ("onclick")} name="{$this->get_variable ("object_name")}" id="{$this->get_variable ("object_name")}" object_id="{$this->get_variable ("object_id")}">{$this->get_variable ("label")}</div>

HTML;
	}

	private function _callback () {

		print <<<HTML
<div class="{$class}" id="{$this->get_variable ("id")}">{$this->get_variable ("label")}</div>

HTML;
	}

	private function _callback_end () {

		print <<<HTML
<div class="{$class}" id="{$this->get_variable ("id")}">{$this->get_variable ("label")}</div>

HTML;
	}

	public function set_callback_begin ($callback) {

		$this->__callback_begin = $callback;
	}

	public function set_callback ($callback) {

		$this->__callback = $callback;
	}

	public function set_callback_end ($callback) {

		$this->__callback_end = $callback;
	}

	public function set_comment_begin ($comment) {

		$this->_comment_begin = $comment;
	}

	public function set_comment_end ($comment) {

		$this->_comment_end = $comment;
	}
}

?>
